package _binary_search;

/**
 * 154. 寻找旋转排序数组中的最小值 II
 */
public class No154 {

    /**
     * 2. 左闭右开（右值为右开区间值）
     */
    public int findMin2(int[] nums) {
        int left = 0, right = nums.length - 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] > nums[right]) left = mid + 1;
            else if (nums[mid] < nums[right]) right = mid;
            else if (nums[mid] == nums[right]) right--;
        }
        return nums[left];
    }

    /**
     * 3. 开区间（右值为右开区间值）
     */
    public int findMin3(int[] nums) {
        int left = -1, right = nums.length - 1;
        while (left + 1 < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] > nums[right]) left = mid;
            else if (nums[mid] < nums[right]) right = mid;
            else if (nums[mid] == nums[right]) right--;
        }
        return nums[right];
    }
}
